﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;

namespace Wasion.HHU.Services
{
    public class BluetoothService
    {
        private StreamSocket streamSocket;
        private DataWriter dataWriter;
        private DataReader dataReader;

        public int UnconsumedBufferLength
        {
            get
            {
                return (int)dataReader.UnconsumedBufferLength;
            }
            internal set { }
        }

        public BluetoothService(StreamSocket chatSocket)
        {
            streamSocket = chatSocket;

            dataWriter = new DataWriter(chatSocket.OutputStream);
            dataReader = new DataReader(chatSocket.InputStream);
        }

        public async Task<uint> WriteStringAsync(string data)
        {
            dataWriter.WriteString(data);
            return await dataWriter.StoreAsync();
            
        }

        public async Task<string> ReadStringAsync(uint length)
        {
            await dataReader.LoadAsync(length);
            return dataReader.ReadString(length);
        }

        public async Task<uint> WriteByteAsync(byte[] data)
        {
            dataWriter.WriteBytes(data);
            return await dataWriter.StoreAsync();

        }

        public async Task<byte[]> ReadByteAsync(uint length)
        {
            await dataReader.LoadAsync(length);
            var b = new byte[length];
            dataReader.ReadBytes(b);

            return b;
        }

        internal DataReaderLoadOperation LoadAsync(int v)
        {
            return dataReader.LoadAsync(1);
        }

        internal void ReadBytes(byte[] buff)
        {
            dataReader.ReadBytes(buff);
        }
    }
}
